package com.xca.is.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xca.is.R;
import com.xca.is.adapter.WallpaperAdapter;
import com.xca.is.databinding.ActivityWallpaperBinding;
import com.xca.is.entity.Wallpaper;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.WallpaperModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WallpaperActivity extends AppCompatActivity implements HttpConnect {
    private ActivityWallpaperBinding binding ;
    private List<Wallpaper.DataDTO> wallpaperList ;
    private WallpaperModel model ;
    private ViewModelProvider provider ;
    private MutableLiveData<Wallpaper> liveData ;
    private WallpaperAdapter adapter ;

    private TextView[] textViews ;
    private int type ;


    private HashMap<String , Integer> map = new HashMap<>() ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityWallpaperBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        textViews = new TextView[]{binding.wallpaperType1, binding.wallpaperType2, binding.wallpaperType3
                , binding.wallpaperType4 , binding.wallpaperType5 , binding.wallpaperType6 , binding.wallpaperType7};
        type = 1 ;
        binding.wallpaperType1.setSelected(true);

        wallpaperList = new ArrayList<>();
        binding.wallpaperRec.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL , false));
        provider = new ViewModelProvider(this) ;
        model = provider.get(WallpaperModel.class) ;
        model.setModel(this);
        liveData = (MutableLiveData<Wallpaper>) model.getLiveData() ;
        liveData.observe(this, new Observer<Wallpaper>() {
            @Override
            public void onChanged(Wallpaper wallpaper) {
                if (wallpaper!=null && adapter != null){
                    wallpaperList = wallpaper.getData() ;
                    adapter.updateData(wallpaperList);
                }
            }
        });


        for (int i = 0; i < textViews.length; i++){
            typeOnClick(i);
        }

        selectPage();

        adapter = new WallpaperAdapter(wallpaperList, this, this);
        binding.wallpaperRec.setAdapter(adapter);
        model.getData(1,1);
    }

    private void typeOnClick(int index){
        binding.wallpaperLoading.rLoading.setVisibility(View.GONE);
        textViews[index].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    typeSelected(index);
                    type = index ;
                    binding.wallpaperPage.setText("1");
                    model.getData(returnType(textViews[index].getText().toString()), Integer.parseInt(binding.wallpaperPage.getText().toString()));
                }
        });
    }

    private void typeSelected(int index){
        for (int i = 0; i < textViews.length; i++){
            textViews[i].setSelected(i == index);
        }
    }

    private void selectPage(){
        binding.wallpaperLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Integer.parseInt(binding.wallpaperPage.getText().toString())<=1){
                    Toast.makeText(WallpaperActivity.this, "已经是第一页",  Toast.LENGTH_SHORT).show();
                }else {
                    binding.wallpaperPage.setText(String.valueOf(Integer.parseInt(binding.wallpaperPage.getText().toString())-1));
                    model.getData(type, Integer.parseInt(binding.wallpaperPage.getText().toString()));
                }
            }
        });
        binding.wallpaperNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.wallpaperPage.setText(String.valueOf(Integer.parseInt(binding.wallpaperPage.getText().toString())+1));
                model.getData(type, Integer.parseInt(binding.wallpaperPage.getText().toString()));
            }
        });
    }


    private int returnType(String type){

        map.put("风景", 1) ;
        map.put("动漫", 2) ;
        map.put("美女", 3) ;
        map.put("游戏", 4) ;
        map.put("明星", 5) ;
        map.put("动物", 6) ;
        map.put("科幻", 7) ;

        return map.getOrDefault(type,1) ;
    }

    @Override
    public void Loading(boolean loading) {
        binding.wallpaperLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {
    }

    @Override
    public void Complete() {
        binding.wallpaperLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}